package playsManager.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.*;

import playsManager.Play;
import playsManager.domainLogic.DomainLogic;
import playsManager.utilities.Utilities;

class ViewerMenu extends UserMenu implements ActionListener{
	
	public ViewerMenu(DomainLogic theDl) {
		super(theDl);
	}
	protected JTextField theater;
	@SuppressWarnings("rawtypes")
	protected JComboBox genre;
	protected JTextField startDate;
	protected JTextField endDate;
	protected JTextField title;
	protected JPanel p;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected void InitMenu() {
		
		//add controls to viewer menu
		p = new JPanel();
		p.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
		JButton enter = new JButton("Enter");
		enter.addActionListener(this);
		String genres[] = {"Genre", "Comedy", "Drama", "History", "Romance"};
		theater = new JTextField("Theater");
		genre = new JComboBox(genres);
		startDate = new JTextField("Start Date: MM/DD/YYYY");
		endDate = new JTextField("End Date: MM/DD/YYYY");
		title = new JTextField("Play Title");
		genre.setSelectedIndex(0);
		p.add(title);
		p.add(theater);
		p.add(startDate);
		p.add(endDate);
		p.add(genre);
		p.add(enter);
		menuFrame.add(p);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		String theaterName, titleString, genreType;
		Date start, end;
		if(theater.getText().compareTo("Theater") == 0){
			theaterName = null;
		}
		else{
			theaterName = theater.getText();
		}
		if(title.getText().compareTo("Play Title") == 0){
			titleString = null;
		}
		else{
			titleString = title.getText();
		}
		if(genre.getSelectedItem().toString().compareTo("Genre") == 0){
			genreType = null;
		}
		else{
			genreType = genre.getSelectedItem().toString();
		}
		if(startDate.getText().compareTo("Start Date: MM/DD/YYYY") == 0){
			start = null;
		}
		else{
			try {
				start = DateFormat.getDateInstance(DateFormat.SHORT).parse(startDate.getText());
			} 
			catch (ParseException e) {
				Utilities.ShowErrorMessage("error parsing date. make sure you type the date in the correct format.");
				e.printStackTrace();
				start = null;
			}
		}
		if(endDate.getText().compareTo("End Date: MM/DD/YYYY") == 0){
			end = null;
		}
		else{
			try {
				end = DateFormat.getDateInstance(DateFormat.SHORT).parse(endDate.getText());
			} 
			catch (ParseException e) {
				Utilities.ShowErrorMessage("error parsing date. make sure you type the date in the correct format.");
				e.printStackTrace();
				end = null;
			}
		}
		p.removeAll();
		String[] columnNames = {"Title", "Theater", "Start Date", "End Date", "Genre", "Description"};
		ArrayList<Play> dbPlays;
		try {
			dbPlays = dl.GetPlays(titleString, theaterName, start, end, genreType);
		
		ArrayList<PlayGuiAdapter> thePlays = new ArrayList<PlayGuiAdapter>();
		for(Play p : dbPlays)
		{
			thePlays.add(new PlayGuiAdapter(p));
		}
		
		Object[][] data = new String[thePlays.size()][6];
		for(int i = 0; i < thePlays.size(); i++){
			data[i][0] = thePlays.get(i).GetName();
			data[i][1] = thePlays.get(i).GetTheaterLocation();
			ArrayList<Date> dates = thePlays.get(i).GetDates();
			data[i][2] = dates.get(0);
			data[i][3] = dates.get(dates.size()-1);
			data[i][4] = thePlays.get(i).GetGenre();
			data[i][5] = thePlays.get(i).GetDescription();
		}
		JTable t = new JTable(data, columnNames);
		p.add(t);
		} catch (Exception e) {
			Utilities.ShowErrorMessage("error loading plays.");
			e.printStackTrace();
		}
	}

}
